﻿@model SDM.Domain.Config.ConfigModel
@{
    ViewBag.Title = "Admin Control Panel";
}
<script type="text/javascript">
    function GetJsonModel(namePrefix)
    {
        // if the name prefix doesnt ends with ".", append one
        if (namePrefix.length > 0 && namePrefix[namePrefix.length - 1])
        {
            namePrefix += ".";
        }
        
        // prepare result
        var result = { };

        // get all inputs in this document
        var inputs = $("input");
        
        // for each input
        for (var i = 0; i < inputs.length; i++)
        {
            // get current input
            var input = inputs[i];
            
            // if input doesnt start from given name           
            if (input.name.substring(0, namePrefix.length) != namePrefix)
            {
                // skip it
                continue;
            }
            
            // take the remaining part
            var propertyName = input.name.substring(namePrefix.length);
            var propertyValue = input.value;
            
            // add to result
            result[propertyName] = propertyValue;
        }
        
        // return result
        return result;
    }

    function GetSqlConfigModel()
    {
        return GetJsonModel("@Html.NameFor(t => t.Sql)");
    }

    function btnTestDatabaseConnection_Clicked()
    {
        $.post('@Url.Action("TestDatabaseConnection")', GetSqlConfigModel(), function (result)
        {
            alert(result);
        });
    }

    function btnFormatDatabase_Clicked()
    {
        runner.run('@Url.Action("FormatDatabase")');
    }
    
    function btnTestDatabase_Clicked()
    {
        runner.run('@Url.Action("TestDatabase")');
    }
</script>
<div>
    <h1>
        Admin Control Panel</h1>
</div>
@using (Html.BeginForm("SaveSettings", "Home"))
{
    if (ViewBag.Result != null)
    {
    <span style="font-weight: bold">Result: @ViewBag.Result</span>
    <hr />
    }
    <fieldset>
        <legend>General Settings</legend>
        <ul>
            <li>
                @Html.LabelFor(t => t.AdminPassword)
                @Html.PasswordFor(t => t.AdminPassword, new { placeholder = "(not changed)" })
            </li>
        </ul>
    </fieldset>
    <fieldset>
        <legend>Database Settings</legend>
        <ul>
            <li>
                @Html.LabelFor(t => t.Sql.ServerName)
                @Html.TextBoxFor(t => t.Sql.ServerName)
            </li>
            <li>
                @Html.LabelFor(t => t.Sql.DatabaseName)
                @Html.TextBoxFor(t => t.Sql.DatabaseName)
            </li>
            <li>
                @Html.LabelFor(t => t.Sql.UserName)
                @Html.TextBoxFor(t => t.Sql.UserName)
            </li>
            <li>
                @Html.LabelFor(t => t.Sql.Password)
                @Html.PasswordFor(t => t.Sql.Password, new { placeholder = "(not changed)" })
            </li>
            <li>
                <label>
                    Test</label>
                <button type="button" onclick="btnTestDatabaseConnection_Clicked()">
                    Test database connection</button>
            </li>
            <li>
                <label>
                    First-time use?</label>
                <button type="button" onclick="btnFormatDatabase_Clicked()">
                    Format database</button>
                    <button type="button" onclick="btnTestDatabase_Clicked()">
                    Test database</button>
            </li>
        </ul>
    </fieldset>
    <input type="submit" value="Save" />
    @Html.ActionLink("Reset to default values", "ResetSettings")
    @:
    @Html.ActionLink("Return to homepage", "Index", "Home", new { area = string.Empty }, null)
}
